package com.sales.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.sales.client.StaffSalesClient;
import com.sales.dao.QuotationMapper;
import com.sales.dao.SalesOrderMapper;
import com.sales.dao.SalesProductMapper;
import com.sales.service.SalesOrderService;
import org.checkerframework.checker.units.qual.A;
import org.comple.erp.pojo.base.enterprise.Staff;
import org.comple.erp.pojo.sales.Quotation;
import org.comple.erp.pojo.sales.SalesOrder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;
import java.util.UUID;

@Service
@Transactional(rollbackFor = RuntimeException.class)
public class SalesOrderServiceImpl implements SalesOrderService {

    @Autowired
    private SalesOrderMapper salesOrderMapper;
    @Autowired
    private SalesProductMapper salesProductMapper;
    @Autowired
    private StaffSalesClient staffSalesClient;
    @Autowired
    private QuotationMapper quotationMapper;

    @Override
    public PageInfo<SalesOrder> salesOrderList(Map<String, Object> params) {

        String pageNumStr = (String) params.get("pageNum");
        String pageSizeStr = (String) params.get("pageSize");

        int pageNum = pageNumStr != null && !pageNumStr.isEmpty() ? Integer.parseInt(pageNumStr) : 1;
        int pageSize = pageSizeStr != null && !pageSizeStr.isEmpty() ? Integer.parseInt(pageSizeStr) : 5;

        PageHelper.startPage(pageNum, pageSize);
        //销售人员
        List<SalesOrder> saleSorderList = salesOrderMapper.selectAll(params);

//        List<Staff> staffList = staffSalesClient.StaffSalesList(params);
//        if (staffList != null) {
//            for (int i = 0; i < saleSorderList.size(); i++) {
//                for (int j = 0; j < staffList.size(); j++) {
//                    if (saleSorderList.get(i).getWorkNumber() == staffList.get(j).getCode()) {
//                        saleSorderList.get(i).setStaffName(staffList.get(j).getName());
//                        break;
//                    }
//                }
//            }
//        }

        //

        PageInfo<SalesOrder> pageInfo = new PageInfo<>(saleSorderList,5);
        return pageInfo;
    }

    @Override
    public Boolean insertSelective(SalesOrder record) {
        Quotation quotation = quotationMapper.selectByPrimaryKey(record.getQuotationId());
        quotation.setSalesOrderCode(record.getCode());
        if(quotationMapper.updateByPrimaryKeySelective(quotation)<=0){
            return false;
        }
        if(salesOrderMapper.insertSelective(record)>0){
            return true;
        }
        return false;
    }

//
    @Override
    public SalesOrder selectByPrimaryKey(String id) {
        SalesOrder salesOrder = salesOrderMapper.selectByPrimaryKey(id);
        return salesOrder;
    }

    @Override
    public Boolean updateSalesOrder(SalesOrder record) {
        if(salesOrderMapper.updateByPrimaryKeySelective(record)>0){
            return true;
        }
        return false;
    }

    @Override
    public Boolean deleteByPrimaryKey(String id) {
        String code = salesOrderMapper.selectByPrimaryKey(id).getCode();
        if(salesProductMapper.selectBySalesProductCode(code) != null){
            salesProductMapper.deleteByCodeLj(code);
        }
        if(salesOrderMapper.deleteByPrimaryKey(id) > 0){
            return true;
        }
        return false;
    }
}
